import 'package:flutter/material.dart';
import 'package:flutter_wanke/user_page.dart';
import 'model/navigation_model.dart';
import 'device_page.dart';
import 'price_page.dart';
import 'monitor_page.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
int decimal = 3;
void main() => runApp(new WankeApp());

class WankeApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: '玩客随行',
      home: new Index(),
      theme: new ThemeData(
        accentColor: Colors.orangeAccent,
        primaryColor: Colors.orangeAccent,
      ),
    );
  }
}

class Index extends StatefulWidget {
  @override
  State<Index> createState() => new _IndexState();
}

class _IndexState extends State<Index> with TickerProviderStateMixin {
  int _currentIndex = 0;
  List<NavigationModel> _navigationViews;
  List<StatefulWidget> _pageList;
  StatefulWidget _currentPage;

  @override
  void initState() {
    super.initState();
    _navigationViews = <NavigationModel>[
      new NavigationModel(
        icon: new Icon(Icons.perm_device_information),
        title: new Text("设备"),
        vsync: this,
      ),
      new NavigationModel(
        icon: new Icon(Icons.camera),
        title: new Text("监控"),
        vsync: this,
      ),
      new NavigationModel(
        icon: new Icon(Icons.show_chart),
        title: new Text("行情"),
        vsync: this,
      ),
      new NavigationModel(
        icon: new Icon(Icons.perm_identity),
        title: new Text("我的"),
        vsync: this,
      ),
    ];
    for (NavigationModel view in _navigationViews) {
      view.controller.addListener(_rebuild);
    }

    _pageList = <StatefulWidget>[
      new DevicePage(),
      new MonitorPage(),
      new PricePage(),
      new UserPage(),
    ];
    _currentPage = _pageList[_currentIndex];
  }

  void _rebuild() {
    setState(() {});
  }

  @override
  void dispose() {
    super.dispose();
    for (NavigationModel view in _navigationViews) {
      view.controller.dispose();
    }
  }

  @override
  Widget build(BuildContext context) {
    final BottomNavigationBar bottomNavigationBar = new BottomNavigationBar(
        items: _navigationViews.map((NavigationModel navigationIconView) => navigationIconView.item).toList(),
        currentIndex: _currentIndex,
        fixedColor: Colors.deepOrange,
        type: BottomNavigationBarType.fixed,
        onTap: (int index) {
          setState(() {
            _navigationViews[_currentIndex].controller.reverse();
            _currentIndex = index;
            _navigationViews[_currentIndex].controller.forward();
            _currentPage = _pageList[_currentIndex];
          });
        });

    return new MaterialApp(
      theme: new ThemeData(
        accentColor: Colors.deepOrange,
        primaryColor: Colors.deepOrange,
      ),
      home: new Scaffold(
        body: new Center(child: _currentPage),
        bottomNavigationBar: bottomNavigationBar,
      ),
    );
  }
}
